package com.parking.carsystem.parkingchargesystem.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.graphics.drawable.DrawableWrapper;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.module.AbsenceData;

import java.util.ArrayList;

import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;


public class BottomDialog extends Dialog {
    View view;
    private RecyclerView event_text_recycle;
    private EXTRecyclerAdapter<AbsenceData> eventAdapter;
    private ChooseTextLisenter chooseTextLisenter;
    private TextView image_name;
    public Context context;
    private WindowManager.LayoutParams params;
    private boolean flag;
    private int type;
    private RecyclerView eventTextRecycle;
    private AbsenceData tempData;
    private TextView textDialog;
    private TextView input_phone;
    private TextView inpu_name;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(int size) {
        this.flag = flag;
    }

    public ChooseTextLisenter getChooseTextLisenter() {
        return chooseTextLisenter;
    }

    public void setChooseTextLisenter(ChooseTextLisenter chooseTextLisenter) {
        this.chooseTextLisenter = chooseTextLisenter;
    }

    public BottomDialog(int size, @NonNull Context context) {
        super(context);
        this.context = context;
        if (size > 10) {
            flag = false;
        } else {
            flag = true;
        }
        init();
    }

    public BottomDialog(int size, @NonNull Context context, int type) {
        super(context);
        this.context = context;
        if (size > 10) {
            flag = false;
        } else {
            flag = true;
        }
        this.type = type;
        init();
    }

    public BottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected BottomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    /**
     * 初始化一些参数
     */
    @SuppressLint("RestrictedApi")
    private void init() {
        if (type == 0) {
            initRadiusView();
        } else if (type == 1) {
            initBottomView();
        } else if (type == 2){
            initGroupText();
        }
        setContentView(view);
        setCanceledOnTouchOutside(false);
        params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = context.getResources().getDimensionPixelOffset(R.dimen.dialog);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        //1,添加这一行代码 dialog 会自动测量高度  根据你设置的背景测量高度 不添加的话,显示不出来  或者在 xml中添加背景,这种设置的recycleView不能滑动,2, 或者直接设置高度 不设置Drawble背景
        //直接给高度, 设置背景为color颜色,但是这个显示不太好看 这种的话 recycleView 可以滑动
//        getWindow().setBackgroundDrawable(new DrawableWrapper(context.getResources().getDrawable(R.drawable.dialog_event)));
        getWindow().setBackgroundDrawable(new ColorDrawable());
        getWindow().setAttributes(params);
    }

    private void initGroupText() {
        view = View.inflate(this.getContext(), R.layout.item_bottomedit_dialog, null);
        inpu_name = view.findViewById(R.id.inpu_name);
        image_name = view.findViewById(R.id.image_name);
        TextView close = view.findViewById(R.id.close);
        TextView sure = view.findViewById(R.id.sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chooseTextLisenter != null) {
                    String inputName = inpu_name.getText().toString().trim();
                    if (!TextUtils.isEmpty(inputName)) {
                        AbsenceData absenceData = new AbsenceData(inputName);
                        chooseTextLisenter.chooseText(absenceData);
                        dismiss();
                    }
                }
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initBottomView() {
        if (flag) {
            view = View.inflate(this.getContext(), R.layout.item_bottom_type_dialog, null);
        } else {
            //解决数据过多把弹窗顶出去了
            view = View.inflate(this.getContext(), R.layout.item_bottom_type_dialog_spare, null);
        }
        //取消
        TextView cancle = view.findViewById(R.id.cancle);
        //确定
        TextView sure = view.findViewById(R.id.sure);
        //和确定一样的功能
        textDialog = view.findViewById(R.id.text_dialog);
        image_name = view.findViewById(R.id.image_name);
        //列表的recycleview
        eventTextRecycle = view.findViewById(R.id.event_text_recycle);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (chooseTextLisenter != null) {
                    chooseTextLisenter.chooseText(tempData);
                }
            }
        });
        textDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (chooseTextLisenter != null) {
                    chooseTextLisenter.chooseText(tempData);
                }
            }
        });
        eventTextRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        eventAdapter = new EXTRecyclerAdapter<AbsenceData>(R.layout.item_dialog_type_text) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, AbsenceData bean) {
                TextView text_dialog = extViewHolder.findViewById(R.id.text_dialog);
                ImageView choose_image = extViewHolder.findViewById(R.id.choose_image);
                text_dialog.setText(bean.text);
                if (bean.choose) {
                    choose_image.setImageResource(R.mipmap.selected);
                } else {
                    choose_image.setImageResource(R.mipmap.unselected);
                }
            }
        };
        eventAdapter.setDatas(null);
        eventAdapter.setOnItemClickListener(new EXTRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, int i) {
                AbsenceData absenceData = eventAdapter.getDatas().get(i);
                if (!absenceData.choose) {
                    if (tempData != null) {
                        tempData.choose = false;
                    }
                    tempData = absenceData;
                    absenceData.choose = true;
                }
                eventAdapter.notifyDataSetChanged();
            }
        });
        eventTextRecycle.setAdapter(eventAdapter);
    }

    private void initRadiusView() {
        if (flag) {
            view = View.inflate(this.getContext(), R.layout.item_bottom_dialog, null);
        } else {
            //解决数据过多把弹窗顶出去了
            view = View.inflate(this.getContext(), R.layout.item_bottom_dialog_spare, null);
        }
        event_text_recycle = (RecyclerView) view.findViewById(R.id.event_text_recycle);
        View cancle = view.findViewById(R.id.text_dialog);
        image_name = view.findViewById(R.id.image_name);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击取消按钮
                dismiss();
            }
        });
        event_text_recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        eventAdapter = new EXTRecyclerAdapter<AbsenceData>(R.layout.item_dialog_text) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, AbsenceData bean) {
                TextView text_dialog = extViewHolder.findViewById(R.id.text_dialog);
                text_dialog.setText(bean.text);
                if (bean.choose) {
                    text_dialog.setTextColor(getContext().getResources().getColor(R.color.cal_color_red));
                } else {
                    text_dialog.setTextColor(getContext().getResources().getColor(R.color.colorPrimary));
                }
                if (eventAdapter.getDatas().size() - 1 == i) {
                    extViewHolder.setVisibility(R.id.line_dialog, View.GONE);
                } else {
                    extViewHolder.setVisibility(R.id.line_dialog, View.VISIBLE);
                }
            }
        };
        eventAdapter.setDatas(null);
        eventAdapter.setOnItemClickListener(new EXTRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, int i) {
                if (chooseTextLisenter != null) {
                    chooseTextLisenter.chooseText(eventAdapter.getDatas().get(i));
                }
//                edt_content.setText(edt_content.getText().toString().trim() + eventRemarksResult.get(i).dataName);
                dismiss();
            }
        });
        event_text_recycle.setAdapter(eventAdapter);
    }

    public void setBottomData(ArrayList<AbsenceData> bottomData) {
        //记录
        if (type == 1 && bottomData.size() > 0) {
            tempData = bottomData.get(0);
        }
        eventAdapter.setDatas(bottomData);
        eventAdapter.notifyDataSetChanged();
    }

    public interface ChooseTextLisenter {
        void chooseText(AbsenceData text);
    }

    public void setTitle(String title) {
        image_name.setText(title);
    }
    public void setBottomTitle(String title) {
        textDialog.setText(title);
    }

    public void dialogDismiss() {
        this.dismiss();
    }
}
